#pragma once

#include <QString>
#include <QMap>
#include "deviceactor.h"
#include "vrdatacore_global.h"
#include "uniformmotion.h"
#include "professionpropertydefine.h"
#include "fadeeffect.h"
#include "templateclass.h"
#include <osg\ref_ptr>

namespace VrDataCore
{
	class VRDATACORE_EXPORT DynamicDataCacheManager
	{
	public:
		struct DynamicObjectData
		{
			QString										_id;
			QString										_endPosition;
			QString										_objectType;
			osg::ref_ptr<DeviceActor>					_deviceActor;
			DeviceActor*								_resourceActor;
			osg::ref_ptr<UniformMotion>					_motionEffect;
			osg::ref_ptr<ProfessionPropertyDefine>		_professionProperty;
			osg::ref_ptr<FadeEffect>					_fadeEffect;
			bool										_removeRequest;
		};
		DynamicDataCacheManager();
		~DynamicDataCacheManager();

		void updatePosition(const QString& xmlstring);
		void setStaticDynamicData(const QString& xmlstring);
		void showDeviceDataWidget(const QString& id);
		void hideDeviceDataWidget();
		void clearAll();
		void mapChanged();
		template<typename T,typename P>
		void registerObjectCreateCallback(const QString& key,void (T::*function)(P),T* obj,P p)
		{
			osg::ref_ptr<FuncBase> newfunc = new Type_1Func<void,T,osg::ref_ptr<CoreBase::ActorBase>>(function,obj,p);
			_createFuncMap.insert(key,newfunc);
		}
		template<typename T,typename P>
		void registerObjectDeleteCallback(const QString& key,void (T::*function)(P),T* obj,P p)
		{
			osg::ref_ptr<FuncBase> newfunc = new Type_1Func<void,T,osg::ref_ptr<CoreBase::ActorBase>>(function,obj,p);
			_deleteFuncMap.insert(key,newfunc);
		}
	protected:
		void create(DynamicObjectData& data,osg::Vec3 pos);
		void update(DynamicObjectData& data,osg::Vec3 pos);
		void remove(DynamicObjectData data);
		void callObjectCreateFun(osg::ref_ptr<CoreBase::ActorBase> p);
		void callObjectDeleteFun(osg::ref_ptr<CoreBase::ActorBase> p);
	private:
		QMap<QString,DynamicObjectData>											_dynamicObjectsPosMap;
		QMap<QString,DynamicObjectData>											_objectsPropertyMap;
		osg::Vec3																_environmentPos;
		QString																	_currentPropertyWidgetId;
		osg::Vec3																_surroundMin;
		osg::Vec3																_surroundMax;
		QMap<QString,osg::ref_ptr<FuncBase>>									_createFuncMap;
		QMap<QString,osg::ref_ptr<FuncBase>>									_deleteFuncMap;
	};
}